package com.qf.student.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Scanner;

/**
 *  数据库的打开和关闭
 * @version1.0
 * @auth dmh
 */
public class DBMangaerUtils {

    private static String JDBC_URL="";
    private static String USERNAME="";
    private static String PASSWORD="";
    private static String DRIVER_CLASS="";

    static{
           //1.加载指定文件中的内容
            Properties properties=PropertiesUtils.loadProperties("jdbc.properties");
            //2.从Properties获取属性
            USERNAME=properties.getProperty("jdbc.username");
            PASSWORD=properties.getProperty("jdbc.password");
            JDBC_URL=properties.getProperty("jdbc.url");
            DRIVER_CLASS=properties.getProperty("jdbc.driverClassName");

        try {
            Class.forName(DRIVER_CLASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库的;连接
     */
    public static Connection getConnection(){
        Connection connection=null;

        try {
            connection= DriverManager.getConnection(JDBC_URL,USERNAME,PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
    /**
     *  万能关闭对象的方法,可关闭所有可关闭的对象
     */
    public static void close(AutoCloseable...args) {
        for(AutoCloseable ob:args){
            if(ob!=null){
                try {
                    ob.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }



}
